﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using Gao.Poker.App.View.Storage;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;
using Gao.Warrior.WPF.Core.Command;

namespace Gao.Poker.App.ViewModel
{
    public class StorageViewModel : BaseViewModel
    {
        #region Command
        /// <summary>
        /// 新增
        /// </summary>
        public ICommand AddCommand { get; private set; }
        /// <summary>
        /// 修改
        /// </summary>
        public ICommand ModifyCommand { get; private set; }
        /// <summary>
        /// 删除
        /// </summary>
        public ICommand DeleteCommand { get; private set; }
        /// <summary>
        /// 保存
        /// </summary>
        public ICommand SaveCommand { get; private set; }
        /// <summary>
        /// 取消
        /// </summary>
        public ICommand CancelCommand { get; private set; }
        /// <summary>
        /// 搜索
        /// </summary>
        public ICommand SearchCommand { get; private set; }
        /// <summary>
        /// 页码改变
        /// </summary>
        public ICommand PageChangeCommand { get; private set; }
        #endregion

        #region View
        /// <summary>
        /// 查看View
        /// </summary>
        private StorageView _view;
        /// <summary>
        /// 编辑View
        /// </summary>
        private StorageEdit _eidt;

        #endregion

        /// <summary>
        /// 库存商品列表
        /// </summary>
        private List<Product> _products;
        /// <summary>
        /// 选中的商品
        /// </summary>
        private Product _Product;

        /// <summary>
        /// 查询参数 商品名称
        /// </summary>
        public String ProductName { get; set; }
        /// <summary>
        /// 查询参数 商品编码
        /// </summary>
        public String BarCode { get; set; }

        protected Int32 PageIndex { get; set; }

        protected Int32 PageSize { get; set; }

        /// <summary>
        /// 主显示区
        /// </summary>
        public ContentControl Content { get; set; }

        public StorageViewModel(ContentControl content)
        {
            ProductName = String.Empty;
            BarCode = String.Empty;
            PageSize = 20;
            Content = content;
            _view = new StorageView();
            _eidt = new StorageEdit();
            AddCommand = new RelayCommand(OnAdd);
            ModifyCommand = new RelayCommand(OnModify);
            DeleteCommand = new RelayCommand(OnDelete);
            SaveCommand = new RelayCommand(OnSave);
            CancelCommand = new RelayCommand(OnCancel);
            SearchCommand = new RelayCommand(OnSearch);
            PageChangeCommand = new RelayCommand(OnPageChange);

            Content.Content = _view;
        }
       
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="obj"></param>
        private void OnSearch(object obj)
        {
            OnPropertyChanged("Products");
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="obj"></param>
        private void OnAdd(object obj)
        {
            Product = null;
            Content.Content = _eidt;
            Product = new Product();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        private void OnModify(object obj)
        {
            if (Product.ID <= 0)
            {
                ShowMessage("选择要修改的商品");
                return;
            }
            Content.Content = _eidt;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="obj"></param>
        private void OnDelete(object obj)
        {
            if(Product.ID <= 0)
            {
                ShowMessage("选择要删除的商品");
                return;
            }
            
            ProductService.Instance().Delete(Product);
            ShowMessage("删除记录成功");
            OnPropertyChanged("Products");
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="obj"></param>
        private void OnSave(object obj)
        {
            if (String.IsNullOrWhiteSpace(Product.BarCode) || String.IsNullOrWhiteSpace(Product.ProductName))
            {
                ShowMessage("商品名称、商品编码不能为空");
                return;
            }
            ProductService.Instance().Save(Product);
            ShowMessage("保存数据成功");
            OnPropertyChanged("Products");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        private void OnPageChange(object obj)
        {
            PageIndex = Convert.ToInt32(obj);
            OnPropertyChanged("Products");
        }


        private void OnCancel(object obj)
        {
            Content.Content = _view;
        }

        /// <summary>
        /// 商品列表
        /// </summary>
        public List<Product> Products
        {
            get
            {
                _products = ProductService.Instance().SearchProducts(ProductName, BarCode, PageIndex, PageSize);
                OnPropertyChanged("ProductTotal");
                return _products;
            }
        }

        public int ProductTotal
        {
            get { return ProductService.Instance().GetProductTotal(ProductName, BarCode); }
        }

        /// <summary>
        /// 选中商品
        /// </summary>
        public Product Product
        {
            get { return _Product; }
            set
            {
                _Product = value;
                OnPropertyChanged("Product");
            }
        }
    }
}
